import java.util.LinkedList;

import org.jfree.data.category.DefaultCategoryDataset;

public class ChartGenerator
{
	public void constructFanOutChart(Project project, boolean relative)
	{
		ChartVersion[] chartVersions = project.getChartVersions();
		// Set chart series data for every ChartVersion we will be charting
		for (ChartVersion ver : chartVersions)
			ver.setSeriesData(getSeries(ver.getVersion(), true));
		// Set chart labels
		String title = "FanOut Histogram";
		String xAxisLabel = "FanOut";
		String yAxisLabel = "FanOut %";
		ChartWrapper cw = ChartWrapper.getInstance();
		cw.createRelativeHistogram(title, xAxisLabel, yAxisLabel, project, false, true,
				relative);
	}

	public void constructFanInChart(Project project, boolean relative)
	{
		ChartVersion[] chartVersions = project.getChartVersions();
		// Set chart series data for every ChartVersion we will be charting
		for (ChartVersion ver : chartVersions)
			ver.setSeriesData(getSeries(ver.getVersion(), false));
		// Set chart labels
		String title = "FanIn Histogram";
		String xAxisLabel = "FanIn";
		String yAxisLabel = "FanIn %";
		ChartWrapper cw = ChartWrapper.getInstance();
		cw.createRelativeHistogram(title, xAxisLabel, yAxisLabel, project, false, true,
				relative);
	}

	public void constructGrowthChart(Project project, int start, int end, int increment,
			String yAxisLabel)
	{
		String title = "defaultTitle";
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		LinkedList<Version> versions = project.getVersionList();

		int yAxisValue = 0;
		String yAxisCategoryLabel = "";

		for (int i = start - 1; i < end; i++)
		{
			Version ver = versions.get(i);

			if (yAxisLabel.equalsIgnoreCase("# classes"))
			{
				yAxisValue += ver.getNumClasses();
				title = "growth by classes";
			}
			else if (yAxisLabel.equalsIgnoreCase("# methods"))
			{
				LinkedList<MetricClass> metricClassList = ver.getMetricClasses();
				for (MetricClass metricClass : metricClassList)
					yAxisValue += metricClass.getMethodCount();
				title = "growth by methods";
			}
			else if (yAxisLabel.equalsIgnoreCase("# packages"))
			{
				yAxisValue += ver.getPackageCount();
				title = "growth by packages";
			}
			else if (yAxisLabel.equalsIgnoreCase("# byte-code"))
			{
				double byteCodeCount = 0;
				LinkedList<MetricClass> metricClassList = ver.getMetricClasses();
				for (MetricClass metricClass : metricClassList)
				{
					byteCodeCount += metricClass.getByteCodeSizeInKb();
					System.out.println(byteCodeCount);
				}
				yAxisValue = (int)(byteCodeCount*1000);
				title = "growth by byte-code";
			}
			
			yAxisCategoryLabel += versions.get(i).getReleaseSequenceNum() + " ";
			
			// check for increment value
			if ((i+1) % increment == 0 || i == end-1)
			{
				dataset.addValue(yAxisValue, "series", yAxisCategoryLabel);
				yAxisValue = 0;
				yAxisCategoryLabel ="";
			}
		}

		String xAxisLabel = "RSN";
		ChartWrapper cw = ChartWrapper.getInstance();
		cw.createGrowthChart(project, title, xAxisLabel, yAxisLabel, false, true, dataset);
	}

	/**
	 * Generates histogram data for the specified version
	 * @param version - The version.
	 * @return - A hashtable containing the cumalative fanout results.
	 */

	private double[] getSeries(Version version, boolean fanOut)
	{
		LinkedList<MetricClass> metricClasses = version.getMetricClasses();
		double[] ret = new double[metricClasses.size()];

		int index = 0;
		for (MetricClass mc : metricClasses)
		{
			if (fanOut)
				ret[index++] = mc.getFanOutCount();
			else
				ret[index++] = mc.getFanInCount();
		}
		return ret;
	}

}
